<?php
declare (strict_types=1);

namespace app\common\logic\navi;

use app\common\model\navi\NaviBottom as NaviBottomModel;

/**
 * 底部导航配置逻辑层
 */
class NaviBottom extends NaviBottomModel
{

    public function getFetchInfo($id)
    {

        $field = 'id,pid,title,content,data_type';

        return $this->where('id', $id)->field($field)->findOrEmpty();
    }

    public function getContent($id)
    {
        return $this->where('id', $id)->value('content');
    }

    public function getTitle($id)
    {
        return $this->where('id', $id)->value('title');
    }

    public function getTree($pid = 0)
    {
        $field = 'id,pid,title,content,data_type';
        $navi  = $this->field($field)->where('pid', $pid)->where('mhm_id', request()->mhm_id)->order(['sort', 'update_time' => 'desc'])->select();
        if (!$navi->isEmpty()) {
            foreach ($navi as $n) {
                if ($this->hasChildren($n->id)) {
                    $n->child = $this->getTree($n->id);
                }
            }
        }
        return $navi;
    }

    // 是否有子集
    public function hasChildren(int $pid = 0): bool
    {
        return (bool)$this->where('pid', $pid)->find();
    }
}
